package tddc77.yahtzee.swing;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import tddc77.yahtzee.Game;
import tddc77.yahtzee.Player;

/**
 *
 * @author John Törnblom (johto012), Mikael Silvén (miksi016)
 */
public class NewGameDialog extends javax.swing.JDialog
{

    private Game game;

    /** Creates new form NewGameDialog */
    public NewGameDialog(java.awt.Frame parent, boolean modal, Game game)
    {
        super(parent, modal);


        initComponents();
        this.game = game;

        // Makes the okButton listen on ENTER presses.
        Action okAction = new AbstractAction()
        {

            public void actionPerformed(ActionEvent e)
            {
                okButtonActionPerformed(e);
            }
        };

        okButton.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
                KeyStroke.getKeyStroke("ENTER"), "okButtonPressed");
        okButton.getActionMap().put("okButtonPressed", okAction);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        player1TextField = new javax.swing.JTextField();
        player1ComboBox = new javax.swing.JComboBox();
        player2ComboBox = new javax.swing.JComboBox();
        player2TextField = new javax.swing.JTextField();
        player3TextField = new javax.swing.JTextField();
        player3ComboBox = new javax.swing.JComboBox();
        player4TextField = new javax.swing.JTextField();
        player4ComboBox = new javax.swing.JComboBox();
        player5TextField = new javax.swing.JTextField();
        player5ComboBox = new javax.swing.JComboBox();
        okButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Configure players");
        setAlwaysOnTop(true);
        setResizable(false);

        player1TextField.setText("Player 1");
        player1TextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                player1TextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                player1TextFieldFocusLost(evt);
            }
        });

        player1ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Human", "Computer" }));

        player2ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Disabled", "Human", "Computer" }));

        player2TextField.setText("Player 2");
        player2TextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                player2TextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                player2TextFieldFocusLost(evt);
            }
        });

        player3TextField.setText("Player 3");
        player3TextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                player3TextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                player3TextFieldFocusLost(evt);
            }
        });

        player3ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Disabled", "Human", "Computer" }));

        player4TextField.setText("Player 4");
        player4TextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                player4TextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                player4TextFieldFocusLost(evt);
            }
        });

        player4ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Disabled", "Human", "Computer" }));

        player5TextField.setText("Player 5");
        player5TextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                player5TextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                player5TextFieldFocusLost(evt);
            }
        });

        player5ComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Disabled", "Human", "Computer" }));

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(player1ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(player1TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(player5ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(player5TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(player2ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(player3ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(player4ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(player4TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                            .addComponent(player3TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                            .addComponent(player2TextField, javax.swing.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)))
                    .addComponent(okButton, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {player1ComboBox, player2ComboBox, player3ComboBox, player4ComboBox, player5ComboBox});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player1ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(player1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player2ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(player2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player3ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(player3TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player4ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(player4TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(player5ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(player5TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {player1ComboBox, player1TextField, player2ComboBox, player2TextField, player3ComboBox, player3TextField, player4ComboBox, player4TextField, player5TextField});

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed

    game.addPlayer(new Player(player1TextField.getText(),
            player1ComboBox.getSelectedIndex() > 0));


    if (player2ComboBox.getSelectedIndex() > 0)
    {
        game.addPlayer(new Player(player2TextField.getText(),
                player2ComboBox.getSelectedIndex() > 1));

    }

    if (player3ComboBox.getSelectedIndex() > 0)
    {
        game.addPlayer(new Player(player3TextField.getText(),
                player3ComboBox.getSelectedIndex() > 1));

    }

    if (player4ComboBox.getSelectedIndex() > 0)
    {
        game.addPlayer(new Player(player4TextField.getText(),
                player4ComboBox.getSelectedIndex() > 1));

    }

    if (player5ComboBox.getSelectedIndex() > 0)
    {
        game.addPlayer(new Player(player5TextField.getText(),
                player5ComboBox.getSelectedIndex() > 1));

    }

    dispose();
}//GEN-LAST:event_okButtonActionPerformed

private void player1TextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_player1TextFieldFocusLost
    if (player1TextField.getText().equals(""))
    {
        player1TextField.setText("Player 1");
    }
}//GEN-LAST:event_player1TextFieldFocusLost

private void player2TextFieldFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player2TextFieldFocusLost
{//GEN-HEADEREND:event_player2TextFieldFocusLost
    if (player2TextField.getText().equals(""))
    {
        player2TextField.setText("Player 2");
    }
}//GEN-LAST:event_player2TextFieldFocusLost

private void player3TextFieldFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player3TextFieldFocusLost
{//GEN-HEADEREND:event_player3TextFieldFocusLost
    if (player3TextField.getText().equals(""))
    {
        player3TextField.setText("Player 3");
    }
}//GEN-LAST:event_player3TextFieldFocusLost

private void player4TextFieldFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player4TextFieldFocusLost
{//GEN-HEADEREND:event_player4TextFieldFocusLost
    if (player4TextField.getText().equals(""))
    {
        player4TextField.setText("Player 4");
    }
}//GEN-LAST:event_player4TextFieldFocusLost

private void player5TextFieldFocusLost(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player5TextFieldFocusLost
{//GEN-HEADEREND:event_player5TextFieldFocusLost
    if (player5TextField.getText().equals(""))
    {
        player5TextField.setText("Player 5");
    }
}//GEN-LAST:event_player5TextFieldFocusLost

private void player1TextFieldFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player1TextFieldFocusGained
{//GEN-HEADEREND:event_player1TextFieldFocusGained
    player1TextField.setText("");
}//GEN-LAST:event_player1TextFieldFocusGained

private void player2TextFieldFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player2TextFieldFocusGained
{//GEN-HEADEREND:event_player2TextFieldFocusGained
    player2TextField.setText("");
}//GEN-LAST:event_player2TextFieldFocusGained

private void player3TextFieldFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player3TextFieldFocusGained
{//GEN-HEADEREND:event_player3TextFieldFocusGained
    player3TextField.setText("");
}//GEN-LAST:event_player3TextFieldFocusGained

private void player4TextFieldFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player4TextFieldFocusGained
{//GEN-HEADEREND:event_player4TextFieldFocusGained
    player4TextField.setText("");
}//GEN-LAST:event_player4TextFieldFocusGained

private void player5TextFieldFocusGained(java.awt.event.FocusEvent evt)//GEN-FIRST:event_player5TextFieldFocusGained
{//GEN-HEADEREND:event_player5TextFieldFocusGained
    player5TextField.setText("");
}//GEN-LAST:event_player5TextFieldFocusGained

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton okButton;
    private javax.swing.JComboBox player1ComboBox;
    private javax.swing.JTextField player1TextField;
    private javax.swing.JComboBox player2ComboBox;
    private javax.swing.JTextField player2TextField;
    private javax.swing.JComboBox player3ComboBox;
    private javax.swing.JTextField player3TextField;
    private javax.swing.JComboBox player4ComboBox;
    private javax.swing.JTextField player4TextField;
    private javax.swing.JComboBox player5ComboBox;
    private javax.swing.JTextField player5TextField;
    // End of variables declaration//GEN-END:variables
}
